package cn.ztydata.teachingsystem.heaven.dao;

import cn.ztydata.teachingsystem.heaven.entity.test.TestExercise;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * 测试或作业习题Dao
 * Created by fanruiping on 2015/3/20.
 */
@Repository
public class TestExerciseDao extends BaseDao {
    /**
     * 批量为作业或测试新增习题
     * @param exercises 习题集合
     * @return int[]
     *
     * @author fanruiping
     * @since 2015-03-20
     */
    public int[] batchAdd(List<TestExercise> exercises){
        String sql = "INSERT INTO t_test_exercise(iTestId,iExerciseId,iParentId,siScore) VALUES(:testId,:exerciseId,:parentId,:score)";

        SqlParameterSource[] batch = SqlParameterSourceUtils.createBatch(exercises.toArray());

        return namedParameterJdbcTemplate.batchUpdate(sql,batch);
    }

    /**
     * 获取指定作业的习题并赋给新的作业
     * @param testId 作业编号
     * @param newTestId 新作业编号
     * @return List
     *
     * @author fanruiping
     * @since 2015-03-21
     */
    public List<TestExercise> getAllForNewTest(Long testId, final Long newTestId){
        String sql = "SELECT iExerciseId,siScore,iParentId FROM t_test_exercise WHERE iTestId=:testId";

        return namedParameterJdbcTemplate.query(sql,new MapSqlParameterSource().addValue("testId",testId),new RowMapper<TestExercise>() {
            @Override
            public TestExercise mapRow(ResultSet rs, int i) throws SQLException {
                TestExercise testExercise = new TestExercise();

                testExercise.setTestId(newTestId);
                testExercise.setExerciseId(rs.getLong("iExerciseId"));
                testExercise.setParentId(rs.getLong("iParentId"));
                testExercise.setScore(rs.getInt("siScore"));

                return testExercise;
            }
        });
    }

    /**
     * 查询作业的习题及分数
     * 
     * @param testId 作业ID
     * @return 习题集合
     * 
     * @author cx
     * @since 2015/3/23
     */
    public List<TestExercise> getExercises(final long testId, boolean hasSon) {
        String where = "";

        //是否获取子题
        if(!hasSon){
            where += " AND iParentId=0";
        }

        String sql = "SELECT iExerciseId,siScore,iParentId FROM t_test_exercise WHERE iTestId=:testId"+where;

        return namedParameterJdbcTemplate.query(sql, new MapSqlParameterSource().addValue("testId", testId), new RowMapper<TestExercise>() {
            @Override
            public TestExercise mapRow(ResultSet rs, int rowNum) throws SQLException {
                TestExercise testExercise = new TestExercise();

                testExercise.setTestId(testId);
                testExercise.setExerciseId(rs.getLong("iExerciseId"));
                testExercise.setScore(rs.getInt("siScore"));
                testExercise.setParentId(rs.getLong("iParentId"));

                return testExercise;
            }
        });
    }

    /**
     * 删除指定作业的所有习题
     * @param testId
     * @return
     */
    public int deleteAllById(Long testId){
        String sql = "DELETE FROM t_test_exercise WHERE iTestId=:testId";

        return namedParameterJdbcTemplate.update(sql,new MapSqlParameterSource().addValue("testId",testId));
    }

    /**
     * 获取作业指定习题分值
     * @param testId 作业编号
     * @param exerciseId 习题编号
     * @return int
     *
     * @author fanruiping
     * @since 2015-04-03
     */
    public int getScore(Long testId, Long exerciseId){
        try{
            String sql = "SELECT siScore FROM t_test_exercise WHERE iTestId=:testId AND iExerciseId=:exerciseId";

            MapSqlParameterSource param = new MapSqlParameterSource();
            param.addValue("testId", testId);
            param.addValue("exerciseId", exerciseId);

            return namedParameterJdbcTemplate.queryForObject(sql, param,Integer.class);
        }catch (EmptyResultDataAccessException ex){
            log.debug("未获取到指定作业的习题信息:EmptyResultDataAccessException ex");

            return 0;
        }
    }
}
